<script>
    eccube.classCategoriesPoint = {{ point_json|raw }};

    // 規格2に選択肢を割り当てる。
    function fnSetClassCategoriesPoint(form, classcat_id2_selected) {
        var $form = $(form);
        var product_id = $form.find('input[name=product_id]').val();
        var $sele1 = $form.find('select[name=classcategory_id1]');
        var $sele2 = $form.find('select[name=classcategory_id2]');
        eccube.setClassCategoriesPoint($form, product_id, $sele1, $sele2, classcat_id2_selected);
    }



    (function(window, undefined) {

        /**
         * 規格2のプルダウンを設定する.
         */
        eccube.setClassCategoriesPoint = function($form, product_id, $sele1, $sele2, selected_id2) {
            if ($sele1 && $sele1.length) {

                if ($sele2 && $sele2.length) {
                    eccube.checkStockPoint($form, product_id, $sele1.val() ? $sele1.val() : '__unselected2',
                        $sele2.val() ? $sele2.val() : '');
                }
            }
        };

        /**
         * 規格の選択状態に応じて, フィールドを設定する.
         */
        eccube.checkStockPoint = function($form, product_id, classcat_id1, classcat_id2) {

            classcat_id2 = classcat_id2 ? classcat_id2 : '';

            var classcat2;

            // 詳細表示時
            if (typeof eccube.classCategoriesPoint[classcat_id1] !== 'undefined') {
                classcat2 = eccube.classCategoriesPoint[classcat_id1]['#' + classcat_id2];
            }
            var $product_point = $('.point-default');
            if (typeof classcat2 === 'undefined') {

                if (typeof this.point_origin === 'undefined') {
                    // 初期値を保持しておく
                    this.point_origin = $product_point.text();
                }
                $product_point.text(this.point_origin);
            } else {

                if (classcat2 && typeof classcat2.point !== 'undefined') {
                    $product_point.text(classcat2.point);
                } else {
                    $product_point.text(this.point_origin);
                }
            }
        };

        /**
         * Initialize.
         */
        $(function() {
            // 規格1選択時
            $('select[name=classcategory_id1]')
                .change(function() {
                    var $form = $(this).parents('form');
                    var product_id = $form.find('input[name=product_id]').val();
                    var $sele1 = $(this);
                    var $sele2 = $form.find('select[name=classcategory_id2]');

                    // 規格1のみの場合
                    if (!$sele2.length) {
                        eccube.checkStockPoint($form, product_id, $sele1.val(), null);
                        // 規格2ありの場合
                    } else {
                        eccube.setClassCategoriesPoint($form, product_id, $sele1, $sele2);
                    }
                });

            // 規格2選択時
            $('select[name=classcategory_id2]')
                .change(function() {
                    var $form = $(this).parents('form');
                    var product_id = $form.find('input[name=product_id]').val();
                    var $sele1 = $form.find('select[name=classcategory_id1]');
                    var $sele2 = $(this);
                    eccube.checkStockPoint($form, product_id, $sele1.val(), $sele2.val());
                });
        });

        {% if form.classcategory_id2 is defined %}
        fnSetClassCategoriesPoint(
            $('#form1'), {{ form.classcategory_id2.vars.value|json_encode|raw }}
        );
        {% elseif form.classcategory_id1 is defined %}
        eccube.checkStockPoint($('#form1'), {{ Product.id }}, {{ form.classcategory_id1.vars.value|json_encode|raw }}, null);
        {% endif %}
    })(window);

</script>